﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate.Linq;

namespace GLAD_prototype
{
    public partial class frmArtist : Form
    {
        public frmArtist()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            artist Artist;
            CRUD.Insert<artist>(Artist = new artist
                                    {
                                        artistID = txtArtistID.Text,
                                        namaArtist = txtArtistName.Text,
                                        descArtist = txtDescription.Text
                                    }
                               );
            CRUD.updateDGV<artist>(dgvArtist);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CRUD.updateDGV<artist>(dgvArtist);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                artist theArtist = session.Query<artist>()
                                   .Where(m => m.artistID == txtArtistID.Text)
                                   .Select(m => m).Single();

                theArtist.namaArtist = txtArtistName.Text;
                theArtist.descArtist = txtDescription.Text;

                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(theArtist);
                    transaction.Commit();
                }
            }

            CRUD.updateDGV<artist>(dgvArtist);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            CRUD.Delete<artist>(m => m.artistID == txtArtistID.Text);
            CRUD.updateDGV<artist>(dgvArtist);
        }

        private void dgvArtist_SelectionChanged(object sender, EventArgs e)
        {
            if ((dgvArtist.Rows.GetFirstRow(DataGridViewElementStates.Selected)) >= 0)
            {
                int idxbar = dgvArtist.Rows.GetFirstRow(DataGridViewElementStates.Selected);
                txtArtistID.Text    = dgvArtist[0, idxbar].Value.ToString().Trim();
                txtArtistName.Text  = dgvArtist[1, idxbar].Value.ToString().Trim();
                txtDescription.Text = dgvArtist[2, idxbar].Value.ToString().Trim();
            }
        }
    }
}
